﻿using Sunboxsoft.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sunboxsoft.ASMS.Server.Domain.Service
{
    public class GetService
    {
        public SingleResult<Data.MaintenanceBillDetail> GetMaintenanceBillDetail(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                View.VMaintenanceBill bill = dbContext.Set<View.VMaintenanceBill>().SingleOrDefault(p => p.Status != Constant.MaintenanceBillStatus.Deleted && p.Status != Constant.MaintenanceBillStatus.Disabled && p.Id == id);
                if (bill == null)
                {
                    return new SingleResult<Data.MaintenanceBillDetail>(1, "object not exist!");
                }

                Data.MaintenanceBillDetail data = new Data.MaintenanceBillDetail();
                data.Bill = bill;
                data.ServiceItemList = dbContext.Set<View.VMaintenanceBillServiceItem>().Where(p => p.MaintenanceBillId == id).OrderBy(p => p.ServiceItemName).ToArray();
                data.MaterialList = dbContext.Set<View.VMaintenanceBillMaterial>().Where(p => p.MaintenanceBillId == id).OrderBy(p => p.MaterialName).ToArray();
                data.PayItemList = dbContext.Set<View.VMaintenanceBillPayItem>().Where(p => p.MaintenanceBillId == id).ToArray();
                return new SingleResult<Data.MaintenanceBillDetail>(data);
            }
        }

        public SingleResult<View.VPackagePurchaseBill> GetPackagePurchaseBill(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                View.VPackagePurchaseBill bill = dbContext.Set<View.VPackagePurchaseBill>().SingleOrDefault(p => p.Id == id);
                if (bill == null)
                {
                    return new SingleResult<View.VPackagePurchaseBill>(1, "bill is not exist");
                }
                return new SingleResult<View.VPackagePurchaseBill>(bill);
            }
        }

        public SingleResult<Data.SettlementBillDetail> GetSettlementBillDetail(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                View.VSettlementBill bill = dbContext.Set<View.VSettlementBill>().SingleOrDefault(p => p.Id == id);
                if (bill == null)
                {
                    return new SingleResult<Data.SettlementBillDetail>(1, "bill not exist");
                }

                Data.SettlementBillDetail detail = GetSettlementBillDetail(dbContext, bill);


                return new SingleResult<Data.SettlementBillDetail>(detail);
            }
        }

        public SingleResult<Data.SettlementBillDetail> GetSettlementBillDetailByMaintennanceBillId(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                View.VSettlementBill bill = dbContext.Set<View.VSettlementBill>().SingleOrDefault(p => p.MaintenanceBillId == id);
                if (bill == null)
                {
                    return new SingleResult<Data.SettlementBillDetail>(1, "bill not exist");
                }

                Data.SettlementBillDetail detail = GetSettlementBillDetail(dbContext, bill);


                return new SingleResult<Data.SettlementBillDetail>(detail);
            }
        }

        private Data.SettlementBillDetail GetSettlementBillDetail(ASMSDbContext dbContext, View.VSettlementBill bill)
        {
            View.VMaintenanceBill maitenanceBill = dbContext.Set<View.VMaintenanceBill>().SingleOrDefault(p => p.Id == bill.MaintenanceBillId);

            Data.SettlementBillDetail detail = new Data.SettlementBillDetail();
            detail.MaintenanceBill = maitenanceBill;
            detail.Bill = bill;

            detail.ServiceItemList = dbContext.Set<View.VSettlementBillServiceItem>().Where(p => p.SettlementBillId == bill.Id).ToArray();
            detail.MaterialList = dbContext.Set<View.VSettlementBillMaterial>().Where(p => p.SettlementBillId == bill.Id).ToArray();
            detail.PayItemList = dbContext.Set<View.VSettlementBillPayItem>().Where(p => p.SettlementBillId == bill.Id).ToArray();
            detail.PackageItemList = dbContext.Set<View.VSettlementBillPackageItem>().Where(p => p.SettlementBillId == bill.Id).ToArray();
            detail.CouponList = dbContext.Set<Entity.ESettlementBillCoupon>().Where(p => p.SettlementBillId == bill.Id).ToArray();
            detail.PayRecordList = dbContext.Set<View.VPayRecord>().Where(p => p.BillId == bill.Id && p.BillType == Constant.BillTypes.SettlementBill).ToArray();


            return detail;
        }

        /// <summary>
        /// 获取充值单详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SingleResult<View.VRechargeBill> GetRechargeBill(Guid id)
        {
            using(ASMSDbContext dbContext = new ASMSDbContext())
            {
                View.VRechargeBill bill = dbContext.Set<View.VRechargeBill>().SingleOrDefault(p => p.Id == id);
                if (bill == null)
                {
                    return new SingleResult<View.VRechargeBill>(1, "bill not exist!");
                }

                return new SingleResult<View.VRechargeBill>(bill);
            }
        }

        public SingleResult<Data.BookingBillDetail> GetBookingBillDetail(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                Data.BookingBillDetail detail = new Data.BookingBillDetail();

                View.VBookingBill bill = dbContext.Set<View.VBookingBill>().SingleOrDefault(p => p.Id == id);
                if (bill == null)
                {
                    return new SingleResult<Data.BookingBillDetail>(1, "bill not exist");
                }
                detail.BookingBill = bill;
                detail.Automobile = dbContext.Set<View.VAutomobile>().SingleOrDefault(p => p.Id == bill.AutomobileId);
                detail.ServiceList = dbContext.Set<View.VBookingService>().Where(p => p.BillId == id).ToArray();
                return new SingleResult<Data.BookingBillDetail>(detail);
            }
        }

        public SingleResult<Entity.EStaff> GetStaffByAccount(string account)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                Entity.EStaff staff = dbContext.Set<Entity.EStaff>().SingleOrDefault(p => p.Account == account);
                if (staff == null)
                {
                    return new SingleResult<Entity.EStaff>(1, "account not exist!");
                }
                return new SingleResult<Entity.EStaff>(staff);
            }
        }

        public SingleResult<View.VStockThresholdValue> GetStockThresholdValue(Guid warehouseId, Guid materialId)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                View.VStockThresholdValue entity = dbContext.Set<View.VStockThresholdValue>().SingleOrDefault(p => p.WarehouseId == warehouseId && p.MaterialId == materialId);
                if (entity == null)
                {
                    return new SingleResult<View.VStockThresholdValue>(1, "object not exist!");
                }
                return new SingleResult<View.VStockThresholdValue>(entity);
            }
        }

        public SingleResult<decimal> GetAsset(Guid customerId, int assetType)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                Entity.EAsset asset = dbContext.Set<Entity.EAsset>().SingleOrDefault(p => p.CustomerId == customerId && p.Type == assetType);
                if (asset == null)
                {
                    return new SingleResult<decimal>(0);
                }
                return new SingleResult<decimal>(asset.Value);
            }
        }

        public SingleResult<Entity.EBusiness> GetBusiness(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                Entity.EBusiness rechargeItem = dbContext.Set<Entity.EBusiness>().SingleOrDefault(p => p.Id == id);
                if (rechargeItem == null)
                {
                    return new SingleResult<Entity.EBusiness>(0, "object not exist");
                }
                return new SingleResult<Entity.EBusiness>(rechargeItem);
            }
        }

        public SingleResult<Entity.ERechargeItem> GetRechargeItem(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                Entity.ERechargeItem rechargeItem = dbContext.Set<Entity.ERechargeItem>().SingleOrDefault(p => p.Id == id);
                if (rechargeItem == null)
                {
                    return new SingleResult<Entity.ERechargeItem>(0, "object not exist");
                }
                return new SingleResult<Entity.ERechargeItem>(rechargeItem);
            }
        }

        public SingleResult<Data.PackageDetail> GetPackageDetail(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                Data.PackageDetail detail = new Data.PackageDetail();
                detail.Package = dbContext.Set<Entity.EPackage>().SingleOrDefault(p => p.Id == id);
                detail.Items = dbContext.Set<Entity.EPackageItem>().Where(p => p.PackageId == id).ToList();
                detail.Prices = dbContext.Set<View.VSalesPrice>().Where(p => p.TargetId == id).ToList();
                return new SingleResult<Data.PackageDetail>(detail);
            }
        }

        /// <summary>
        /// 获取客户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SingleResult<View.VCustomer> GetCustomer(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                View.VCustomer customer = dbContext.Set<View.VCustomer>().SingleOrDefault(p => p.Id == id);
                if(customer != null)
                {
                    return new SingleResult<View.VCustomer>(customer);
                }
                return new SingleResult<View.VCustomer>(1, "customer not exist!");
            }
        }

        /// <summary>
        /// 获取仓库数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SingleResult<View.VWarehouse> GetWarehouse(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                View.VWarehouse warehouse = dbContext.Set<View.VWarehouse>().SingleOrDefault(p => p.Id == id);
                if (warehouse != null)
                {
                    return new SingleResult<View.VWarehouse>(warehouse);
                }
                return new SingleResult<View.VWarehouse>(1, "warehouse not exist!");
            }
        }
    }
}
